//
//  HDSearchBar.swift
//  Sina
//
//  Created by 王亮龙1 on 16/5/20.
//  Copyright © 2016年 lianglong. All rights reserved.
//

import UIKit

class HDSearchBar: UIView {

    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var searchTF: MYTextField!
    
    @IBOutlet weak var rightCons: NSLayoutConstraint!
    
    
    //加载视图的方法
    class func loadHDSearchBar() -> HDSearchBar {
        
        let searchView = NSBundle.mainBundle().loadNibNamed("HDSearchBar", owner: nil, options: nil).last as! HDSearchBar
        
        return searchView
        
    }
    
    //布局子控件
    override func awakeFromNib() {
        
        self.bounds.size.width = ScreenWidth
        
        searchTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        searchTF.leftViewMode = .Always
        searchTF.leftView?.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
        searchTF.leftView?.contentMode = .Center
        
        
    }
    
    
    @IBAction func searchTFBeginEditing(sender: AnyObject) {
        
        UIView.animateWithDuration(0.5) {
            self.rightCons.constant = self.cancelBtn.bounds.width
            
            self.layoutIfNeeded()
        }
        
    }
    
    
    @IBAction func cancelBtnClick(sender: AnyObject) {
        
        UIView.animateWithDuration(0.5) {
            self.rightCons.constant = 0
            
            self.layoutIfNeeded()
        }
        
        self.endEditing(true)
    }
    
    
    
    
}
